/**
 * 
 */
package com.googlecode.agiletools.codereview.action;

import java.util.List;

import com.googlecode.agiletools.codereview.ReviewRecord;
import com.googlecode.agiletools.codereview.ReviewRecordManager;
import com.opensymphony.xwork2.Action;

/**
 * @author Administrator
 * 
 */
public class ReviewRecordListAction {

    private static final int ROW_PER_PAGE = 20;

    private int page = 1;

    private int totalPages;

    private ReviewRecordManager reviewRecordManager;

    private List<ReviewRecord> reviewRecords;

    private String file = null;

    private Integer revision = null;

    private String tag = null;

    /**
     * @return the page
     */
    public int getPage() {
	return page;
    }

    public int getNextPage() {
	return Math.min(page + 1, totalPages);
    }

    public int getPreviousPage() {
	return Math.max(page - 1, 0);
    }

    /**
     * @param page
     *            the page to set
     */
    public void setPage(int page) {
	this.page = page;
    }

    /**
     * @return the reviewRecords
     */
    public List<ReviewRecord> getReviewRecords() {
	return reviewRecords;
    }

    public String list() {
	if (tag != null) {
	    return Action.NONE;
	} else {
	    reviewRecords = reviewRecordManager.find(file, revision,
		    ROW_PER_PAGE * (page - 1), ROW_PER_PAGE);
	}
	totalPages = (int) Math.ceil(Double.valueOf(reviewRecordManager
		.totalCount())
		/ Double.valueOf(ROW_PER_PAGE));
	return Action.SUCCESS;
    }

    /**
     * @return the file
     */
    public String getFile() {
        return file;
    }

    /**
     * @param file the file to set
     */
    public void setFile(String file) {
        this.file = file;
    }

    /**
     * @return the revision
     */
    public Integer getRevision() {
        return revision;
    }

    /**
     * @param revision the revision to set
     */
    public void setRevision(Integer revision) {
        this.revision = revision;
    }

    /**
     * @return the tag
     */
    public String getTag() {
        return tag;
    }

    /**
     * @param tag the tag to set
     */
    public void setTag(String tag) {
        this.tag = tag;
    }

    /**
     * @return the totalPages
     */
    public int getTotalPages() {
	return totalPages;
    }

    /**
     * @param reviewRecordManager
     *            the reviewRecordManager to set
     */
    public void setReviewRecordManager(ReviewRecordManager reviewRecordManager) {
	this.reviewRecordManager = reviewRecordManager;
    }

}
